package com.haiyou.data.game.base;

import com.haiyou.common.constants.ErrCode;
import com.haiyou.common.util.check.Assert;
import com.haiyou.data.common.accessor.Accessor;
import com.haiyou.data.common.accessor.redis.redisson.RedissonAccessor;
import com.haiyou.data.common.accessor.redis.redisson.bucket.BucketAccessor;
import com.haiyou.data.common.cache.redis.RKeys;

/**
 * 
 * 
 * @Description: 玩家模块转换器
 * @author xingyuan
 * @date 2022年2月19日 下午2:13:24
 */
public class GamePlayerModuleCaster {

	
	public static String castServerIdTo(int serverId, Class<?> entityClass) {
	
		Accessor<String,?> accessor = Accessor.getInstance(entityClass);
		
		String keyPrefix = null;
		
		if(accessor instanceof RedissonAccessor) {
			keyPrefix = ((RedissonAccessor<String, ?>)accessor).getKeyPrefix();
		}else if(accessor instanceof BucketAccessor){
			keyPrefix = ((BucketAccessor<?>)accessor).getKeyPrefix();
		}

		//错误
		Assert.isTrue(keyPrefix !=null, ErrCode.NONE_KNOW_ERROR);
		
		String id = RKeys.createRId(keyPrefix, serverId);

		return id;
	}
	
	

	/**
	 * id转换
	 * 
	 * @param <T>
	 * @param playerId
	 * @param entityClass
	 * @return
	 */
	public static String castPlayerIdTo(long playerId, Class<?> entityClass) {
		Accessor<String,?> accessor = Accessor.getInstance(entityClass);
		
		String keyPrefix = null;
		
		if(accessor instanceof RedissonAccessor) {
			keyPrefix = ((RedissonAccessor<String, ?>)accessor).getKeyPrefix();
		}else if(accessor instanceof BucketAccessor){
			keyPrefix = ((BucketAccessor<?>)accessor).getKeyPrefix();
		}

		//错误
		Assert.isTrue(keyPrefix !=null, ErrCode.NONE_KNOW_ERROR);
		
		String id = RKeys.createRId(keyPrefix, playerId);
		
		return id;
	}

}
